﻿using System;
using System.Windows;
using System.Windows.Media;
using System.Windows.Threading;
using System.Diagnostics;
using Microsoft.Research.DynamicDataDisplay;
using Microsoft.Research.DynamicDataDisplay.DataSources;
using Microsoft.Research.DynamicDataDisplay.PointMarkers;
using System.Data;
using System.Data.SqlClient;

namespace Davian
{
    /// <summary>
    /// Interaction logic for WpfDrawing.xaml
    /// </summary>
    public partial class WpfDrawing : Window
    {
        private DispatcherTimer timer = new DispatcherTimer();
        private PerformanceCounter cpuPerformance = new PerformanceCounter();
        private ObservableDataSource<Point> dataSource = new ObservableDataSource<Point>(30);
        private ObservableDataSource<Point> dataSource1 = new ObservableDataSource<Point>(30);

        public WpfDrawing()
        {
            InitializeComponent();   
        }

        private void InitializeDispatcherTimer()
        {
            //plotter.AddLineGraph(dataSource, Colors.Green, 2, "Percentage");
            plotter.AddLineGraph(dataSource,
                                 new Pen(Brushes.Blue, 2),
                                 new CirclePointMarker { Size = 10.0, Fill = Brushes.Red },
                                 new PenDescription("CPU Percentage"));

            plotter.AddLineGraph(dataSource1,
                                 new Pen(Brushes.Green, 2),
                                 new TrianglePointMarker{
                                                             Size = 10.0,
                                                             Pen = new Pen(Brushes.Black, 2.0),
                                                             Fill = Brushes.GreenYellow
                                 },
                                 new PenDescription("CPU Percentage 1"));
            
            timer.Interval = TimeSpan.FromSeconds(1);
            timer.Tick += new EventHandler(dt_AnimatedPlot);
            timer.IsEnabled = true;
            
            plotter.Viewport.FitToView();
        }

        private void dt_AnimatedPlot(object sender, EventArgs e)
        {
            AnimatedPlot();
        }

        private void Window_Loaded(object sender, RoutedEventArgs e)
        {
            InitializeDispatcherTimer();
            try
            {
                String queryString = "select count(*) as count from RIDE_DICT;";
                String connectionString = (new SysXml()).ReadSqlserver().ConnectionString;

                SqlConnection conn = new SqlConnection(connectionString);
                DataSet ds = new DataSet();
                DataTable dt = new DataTable();
                SqlDataAdapter da = new SqlDataAdapter(queryString, conn);
                da.Fill(dt);
                Console.WriteLine(dt.Rows[0]["count"]);
                /*
                using (SqlConnection connection = new SqlConnection(connectionString))
                {
                    SqlCommand command = new SqlCommand(queryString, connection);
                    connection.Open();
                    SqlDataReader reader = command.ExecuteReader();
                    try
                    {
                        while (reader.Read())
                        {
                            Console.WriteLine(String.Format("{0}, {1}",
                                reader[0], reader[1]));
                        }
                    }
                    finally
                    {
                        // Always call Close when done reading.
                        reader.Close();
                    }
                }*/
            }
            catch (Exception ex)
            {
                Console.WriteLine("Caught exception #2.");
                String a = ex.Message;
                a = a;
            }
            finally
            {
                Console.WriteLine("Executing finally block.");
            }
        }

        private void AnimatedPlot()
        {
            cpuPerformance.CategoryName = "Processor";
            cpuPerformance.CounterName = "% Processor Time";
            cpuPerformance.InstanceName = "_Total";

            double x = dateAxis.ConvertToDouble(DateTime.Now);
            double y = cpuPerformance.NextValue();

            Point point = new Point(x, y);
            dataSource.AppendAsync(base.Dispatcher, point);
            dataSource.RemoveAsync(base.Dispatcher);

            Point point1 = new Point(x, y - 5);
            dataSource1.AppendAsync(base.Dispatcher, point1);
            dataSource1.RemoveAsync(base.Dispatcher);

            cpuUsageText.Text = String.Format("{0:0}%", y);
        }

    }
}
